package com.lanyanhua.job.controller;

import com.lanyanhua.job.entity.EmailServerInfo;
import com.lanyanhua.job.entity.Page;
import com.lanyanhua.job.entity.Result;
import com.lanyanhua.job.service.IEmailService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Auther: lanyanhua
 * @Date: 2018/10/31 0031 10:10
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("/email")
public class EmailController  {


    private IEmailService emailService;


    @Autowired
    public EmailController(IEmailService emailService) {
        this.emailService = emailService;
    }

    /**
     * 联动查询 分组下所有的邮箱信息
     */
    @GetMapping("/findGroupEmail")
    public List<EmailServerInfo> findGroupEmail(String jobGroup){
        return emailService.findGroupEmail(jobGroup);
    }

    /**
     * 查询邮箱
     */
    @GetMapping(value="/findJobEmailPage")
    public Page findJobEmailPage(EmailServerInfo emailServerInfo, Integer page, Integer limit)
    {
        return emailService.findJobEmailPage(emailServerInfo,page-1,limit);
    }

    /**
     * 保存邮箱
     */
    @PostMapping("/saveEmail")
    public Result<String> saveEmail(EmailServerInfo emailServerInfo){
        try {
            emailService.saveAndFlush(emailServerInfo);
            return new Result<>(true,"保存成功!");
        }catch (Exception e){
            log.error("保存邮箱数据报错："+ExceptionUtils.getStackTrace(e));
            return new Result<>(false,"保存失败："+e.getMessage());
        }
    }

    /**
     * 删除邮箱
     */
    @PostMapping("deleteEmail")
    public Result<String> deleteEmail(Integer emailId){
        try {
            emailService.deleteById(emailId);
            return new Result<>(true,"删除成功!");
        }catch (Exception e){
            log.error("删除邮箱数据报错：" + ExceptionUtils.getStackTrace(e));
            return new Result<>(false,"删除失败："+e.getMessage());
        }
    }



}
